package com.animations;

import com.beans.ButtonEvent;
import com.beans.ListBean;
import com.beans.PersonBean;
import com.beans.ScoresListBean;
import creator.ui.controller.ReadData;
import creator.ui.graphics.Magicanvas;
import creator.ui.graphics.Paintools;
import creator.ui.graphics.bean.Switch;
import creator.ui.graphics.polaroid.Animation;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ListAction extends Animation {

  protected ListBean list;
  private ScoresListBean slist;
  protected boolean isDisplay = false;
  private boolean isDisScores = false;
  private int oldY;
  private int nowY;
  private static int maxSub;
  private static List<PersonBean> peps;
  private static final int fontSize = 20;
  private static final Font DefaultFont = new Font("tama mini02", 0, 20);
  private static final int contextX = 35;
  private static final int contextY = 45;
  private static final int Distance = 7;

  public void loadBean() {
    this.list = new ListBean();
    this.slist = new ScoresListBean();
    getBeans().add(this.list);
    getBeans().add(this.slist);
  }

  public void paint(Graphics g) {
    paintList(g);
    paintList0(g);
  }

  public void paintList(Graphics g) {
    paintList1(g);
  }

  public void paintList1(Graphics g) {
    g.drawImage(
      this.list.getList(),
      this.list.getListX(),
      this.list.getListY(),
      getMagicanvas()
    );
  }

  private void paintList0(Graphics g) {
    g.drawImage(
      this.slist.getSlist(),
      this.slist.getSlistX(),
      this.slist.getSlistY(),
      getMagicanvas()
    );

    if (this.isDisScores) {
      if (peps.size() > 15) {
        maxSub = 15;
        Paintools.drawShadowString(
          "... More than " + (peps.size() - 15),
          this.slist.getSlistX() + 35,
          (int) (this.slist.getSlistY() + 310.0D + 45.0D),
          DefaultFont,
          1,
          Color.WHITE,
          Color.BLACK,
          g
        );
      } else {
        maxSub = peps.size();
      }
      for (int i = 0; i < maxSub; i++) {
        Paintools.drawShadowString(
          i + 1 + ".",
          this.slist.getSlistX() + 35,
          this.slist.getSlistY() + i * 20 + 45 + 7,
          DefaultFont,
          1,
          Color.WHITE,
          Color.BLACK,
          g
        );
        Paintools.drawShadowString(
          ((PersonBean) peps.get(i)).time,
          this.slist.getSlistX() + 35 + 70,
          this.slist.getSlistY() + i * 20 + 45 + 7,
          DefaultFont,
          1,
          Color.WHITE,
          Color.BLACK,
          g
        );
        Paintools.drawShadowString(
          ((PersonBean) peps.get(i)).scores,
          this.slist.getSlistX() + 35 + 200,
          this.slist.getSlistY() + i * 20 + 45 + 7,
          DefaultFont,
          1,
          Color.WHITE,
          Color.BLACK,
          g
        );
      }
    }
  }

  public void run() {
    this.oldY = this.slist.getSlistY();
    this.nowY = 614;
    this.slist.setSlistY(this.oldY + this.nowY);
    getMagicanvas().repaint();
    LoadMouseListener(getMagicanvas());
    doList();
  }

  private void doList() {
    while (Switch._isContinue()) {
      if (this.isDisplay) {
        DiplayListBoard();
        getMagicanvas().repaint();
      } else {
        HiddenListBoard();
        getMagicanvas().repaint();
      }
      try {
        Thread.sleep(500L);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  private void DiplayListBoard() {
    loadScores();
    while ((this.nowY -= 2 >= this.oldY) && (this.isDisplay)) {
      this.slist.setSlistY(this.nowY);
      getMagicanvas().repaint();
      try {
        Thread.sleep(1L);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  private void loadScores() {
    if (peps == null) {
      String uri = getClass()
        .getResource("/res")
        .getPath()
        .replace("file:/", "")
        .replace("FlappyBird.jar!/", "")
        .replace("/res", "/Scores.dat");
      File sf = new File(uri);
      if (!sf.exists()) {
        try {
          sf.createNewFile();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    ArrayList Persons = (ArrayList) ReadData.ReadObjectFromLocal("/Scores.dat");
    if (Persons == null) {
      Persons = new ArrayList();
      Persons.add(new PersonBean(MedalsBoardAction.getTime(), 0));
      MedalsBoardAction.AppendNewPerson(Persons);
    }
    sortScores(Persons);
    this.isDisScores = true;
  }

  private void sortScores(ArrayList<PersonBean> persons) {
    peps = persons;
    Collections.sort(peps, new SortByScores());
  }

  private void HiddenListBoard() {
    this.isDisScores = false;
    while ((this.nowY += 2 <= 614) && (!this.isDisplay)) {
      this.slist.setSlistY(this.nowY);
      getMagicanvas().repaint();
      try {
        Thread.sleep(1L);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  public void LoadMouseListener(Magicanvas magicanvas) {
    magicanvas.addMouseListener(
      new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
          if (
            (e.getX() > ListAction.this.list.getListX()) &&
            (
              e.getX() <
              ListAction.this.list.getListX() +
              ListAction.this.list.getListW()
            ) &&
            (e.getY() > ListAction.this.list.getListY()) &&
            (
              e.getY() <
              ListAction.this.list.getListY() +
              ListAction.this.list.getListH()
            )
          ) ListAction.this.list.setListY(
              ListAction.this.list.getListOldY() + 3
            );
        }

        public void mouseReleased(MouseEvent e) {
          if (
            (e.getX() > ListAction.this.list.getListX()) &&
            (
              e.getX() <
              ListAction.this.list.getListX() +
              ListAction.this.list.getListW()
            ) &&
            (e.getY() > ListAction.this.list.getListY()) &&
            (
              e.getY() <
              ListAction.this.list.getListY() +
              ListAction.this.list.getListH()
            )
          ) {
            ListAction.this.list.setListY(ListAction.this.list.getListOldY());
            ButtonEvent.sendMassage("-> Clicked List");
            ListAction.this.isDisplay = true;
            ListAction.this.getMagicanvas().repaint();
          } else {
            ListAction.this.isDisplay = false;
            ListAction.this.getMagicanvas().repaint();
          }
        }
      }
    );
  }
}
